import 'package:flutter/material.dart';
import 'package:flutter/physics.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: SpringAnimationExample());
  }
}

class SpringAnimationExample extends StatefulWidget {
  @override
  _SpringAnimationExampleState createState() => _SpringAnimationExampleState();
}

class _SpringAnimationExampleState extends State<SpringAnimationExample>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late SpringSimulation _simulation;

  @override
  void initState() {
    super.initState();

    // 创建AnimationController，注意这里使用unbounded，因为模拟可能会产生超出[0,1]的值
    _controller = AnimationController.unbounded(vsync: this);

    // 创建SpringSimulation
    _simulation = SpringSimulation(
      SpringDescription(
        mass: 1.0, // 质量
        stiffness: 100.0, // 刚度
        damping: 10.0, // 阻尼
      ),
      100.0, // 起始位置
      0.0, // 目标位置
      10.0, // 起始速度
    );

    // 启动动画
    _controller.animateWith(_simulation);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Spring Animation Example')),
      body: Center(
        child: Transform.translate(
          offset: Offset(0.0, _controller.value),
          child: Container(width: 100.0, height: 100.0, color: Colors.blue),
        ),
      ),
    );
  }
}
